﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace Manager.Configuration
{
    /// <summary>
    /// Configuration file parser class
    /// </summary>
    class ConfigParser
    {
        string m_Path;

        /// <summary>
        /// Gets or sets the configuration file path.
        /// </summary>
        /// <value>The path.</value>
        public string Path
        {
            get { return m_Path; }
            set { m_Path = value; }
        }

        string currentConfig;

        /// <summary>
        /// Reads the log.
        /// </summary>
        public void ReadLog()
        {
            StreamReader reader = new StreamReader(m_Path);
            currentConfig = reader.ReadToEnd();
        }

        /// <summary>
        /// Parses the logging section.
        /// </summary>
        /// <returns></returns>
        public virtual LoggingStatement ParseLogging()
        {
            string pattern = @"logging\s*\{(.|\s)*\};";
            Regex regEx = new Regex(pattern);

            string loggingSection = regEx.Match(currentConfig).Value;

            LoggingStatement loggingStatement = new LoggingStatement();
            loggingStatement.File = ParseFilePart(loggingSection);

            return loggingStatement;
        }

        /// <summary>
        /// Parses the file part of the logging section.
        /// </summary>
        /// <param name="loggingSection">The logging section.</param>
        /// <returns></returns>
        protected virtual FilePart ParseFilePart(string loggingSection)
        {
            FilePart file = new FilePart();
            
            // file\s*\" (?<path>.+) \"\s*((?<versions>versions\s+(?<versionValue>\d+))*\s+((?<size>size\s+(?<sizeValue>\w+)))*)*;
            string regex = "file\\s*\\\" (?<path>.+) \\\"\\s*((?<versions>versions\\s+(?<versionValue>\\d+))*\\s+((?<s" +
                "ize>size\\s+(?<sizeValue>\\w+)))*)*";
            System.Text.RegularExpressions.RegexOptions options = (System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace | System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(regex, options);
            Match fileMatch = reg.Match(loggingSection);
            
            if (fileMatch.Success)
            {
                file.Path = fileMatch.Groups["path"].Value;
                int fileVersions;
                int.TryParse(fileMatch.Groups["versionValue"].Value, out fileVersions);
                file.Versions = fileVersions == 0 ? int.MinValue : fileVersions;
                long size;
                long.TryParse(fileMatch.Groups["sizeValue"].Value, out size);
                file.Size = size == 0 ? long.MinValue : size;
            }

            return file;
            
        }
    }
}
